<template>
	<!-- 规格弹窗 -->
	<su-popup :show="show" round="10" @close="emits('close')">
		<view class="ss-modal-box bg-white ss-flex-col">
			<view class="modal-header ss-flex ss-col-center">
				<view class="header-left ss-m-r-30">
					<image
						class="sku-image"
						:src="$url.cdn(state.selectedSkuPrice.image || goodsInfo.image)"
						mode="aspectFill"
					></image>
				</view>
				<view class="header-right ss-flex-col ss-row-between ss-flex-1">
					<view class="goods-title ss-line-2">{{ goodsInfo.title }}</view>
					<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
						<view class="ss-flex">
							<view v-if="goodsPrice.price > 0" class="price-text">
								{{ goodsPrice.price }}
							</view>
							<view class="ss-flex">
								<view
									v-if="goodsPrice.price > 0 && goodsPrice.score > 0"
									class="score-text ss-m-l-4"
									>+
								</view>
								<image
									v-if="goodsPrice.score > 0"
									:src="$url.static('/static/img/shop/goods/score1.svg')"
									class="score-img"
								>
								</image>
								<view v-if="goodsPrice.score > 0" class="score-text">
									{{ goodsPrice.score }}
								</view>
							</view>
						</view>

						<view class="stock-text ss-m-l-20">
							{{
								state.selectedSkuPrice.stock
									? formatStock(
											goodsInfo.stockShowType,
											state.selectedSkuPrice.stock,
									  )
									: formatStock(goodsInfo.stockShowType, goodsInfo.stock)
							}}
						</view>
					</view>
				</view>
			</view>

			<!-- 展示规格属性 -->
			<view class="modal-content ss-flex-1">
				<scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
					<view
						class="sku-item ss-m-b-20"
						v-for="sku1 in goodsInfo.attrs"
						:key="sku1.title"
					>
						<view class="label-text ss-m-b-20">{{ sku1.title }}</view>
						<view class="ss-flex ss-col-center ss-flex-wrap">
							<button
								class="ss-reset-button spec-btn"
								v-for="sku2 in sku1.children"
								:class="[
									{
										'ui-BG-Main-Gradient':
											state.currentSkuArray[sku1.title] == sku2.title,
									},
									{
										'disabled-btn': sku2.disabled == true,
									},
								]"
								:key="sku2.title"
								:disabled="sku2.disabled == true"
								@tap="onSelectSku(sku1.title, sku2.title)"
							>
								{{ sku2.title }}
							</button>
						</view>
					</view>
					<!--   -->
					<view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40">
						<view class="label-text">购买数量</view>
						<su-number-box
							:min="1"
							:max="state.selectedSkuPrice.stock"
							:step="1"
							v-model="state.selectedSkuPrice.goodsNum"
							@change="onNumberChange($event)"
						></su-number-box>
					</view>
				</scroll-view>
			</view>
			<view class="modal-footer border-top">
				<view
					class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center"
					v-if="isScore"
				>
					<!-- 积分兑换.... -->
					<button class="ss-reset-button score-btn ui-Shadow-Main" @tap="onBuy"
						>立即兑换</button
					>
				</view>
				<view
					class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center"
					v-else
				>
					<button class="ss-reset-button add-btn ui-Shadow-Main" @tap="onAddCart"
						>加入购物车</button
					>
					<button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy"
						>立即购买</button
					>
				</view>
			</view>
		</view>
	</su-popup>
</template>

<script setup>
	import { computed, reactive, watch } from 'vue';
	import { formatStock, formatPrice } from '/shop/hooks/useGoods';
	import { isEmpty } from 'lodash';
	import $helper from '/shop/helper/index.js';
	import $url from '/shop/url/index.js';
	import _ from 'lodash';

	const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
	const props = defineProps({
		goodsInfo: {
			type: Object,
			default() {},
		},
		show: {
			type: Boolean,
			default: false,
		},
		isScore: {
			type: Boolean,
			default: false,
		},
	});

	const state = reactive({
		selectedSkuPrice: {},
		currentSkuArray: {},
	});
	//输入框改变数量
	function onNumberChange(e) {
		if (e === 0) return;
		if (state.selectedSkuPrice.goodsNum === e) return;
		state.selectedSkuPrice.goodsNum = e;
	}

	// 默认单规格  判断是否为单规格，如果是不是单规格默认去第一个当做初始化
	if (props.goodsInfo.spec === 1) {
		state.selectedSkuPrice = props.goodsInfo.goodsSkus[0];
		state.selectedSkuPrice.skus.forEach((e) => {
			state.currentSkuArray[e.key] = e.value;
		});
		state.selectedSkuPrice.goodsNum = 1;
	} else {
		// 构建同一个属性方便处理
		state.selectedSkuPrice = {
			stock: props.goodsInfo.stock,
			goodsNum: 1,
			price: props.goodsInfo.price,
		};
		//  如果是单规格的直接使用商品本身属性，需要构建商品默认的selectedSkuPrice 价格 库存内容....
	}

	const skus = props.goodsInfo.attrs;

	watch(
		() => state.selectedSkuPrice,
		(newVal) => {
			emits('change', newVal);
		},
		{
			immediate: true, // 立即执行
			deep: true, // 深度监听
		},
	);

	const goodsPrice = computed(() => {
		let price, score;
		if (isEmpty(state.selectedSkuPrice)) {
			price = props.goodsInfo.price;
			score = props.goodsInfo.score || 0;
		} else {
			console.log('skus is  not  null');
			price = state.selectedSkuPrice.price;
			score = state.selectedSkuPrice.score || 0;
		}
		return {
			price,
			score,
		};
	});

	function onAddCart() {
		if (state.selectedSkuPrice.stock <= 0) {
			$helper.toast('库存不足');
		} else {
			emits('addCart', state.selectedSkuPrice);
		}
	}

	function onBuy() {
		if (state.selectedSkuPrice.stock <= 0) {
			$helper.toast('库存不足');
		} else {
			emits('buy', state.selectedSkuPrice);
		}
	}

	// 当前所选规格下，获取所有有库存的 skuPrice
	function getCanUseSkuPrice() {
		// let condtion = state.currentSkuArray.
		let goodSkuPrice = _.find(props.goodsInfo.goodsSkus, (e) => {
			let skus = e.skus;
			return _.every(skus, (kv) => {
				let key = kv.key;
				let value = kv.value;
				let skuv = state.currentSkuArray[key];
				return value == skuv;
			});
		});
		console.log('选中的skus: ', goodSkuPrice);
		return goodSkuPrice;
	}
	// 选择规格
	function onSelectSku(key, value) {
		console.log('key: ', key, ' , value: ', value);
		state.currentSkuArray[key] = value;
		// // 当前所选规格下，所有可以选择的 skuPric
		state.selectedSkuPrice = getCanUseSkuPrice();
	}

	// changeDisabled(false);
</script>

<style lang="scss" scoped>
	// 购买
	.buy-box {
		padding: 10rpx 0;

		.add-btn {
			width: 356rpx;
			height: 80rpx;
			border-radius: 40rpx 0 0 40rpx;
			background-color: var(--ui-BG-Main-light);
			color: var(--ui-BG-Main);
		}

		.buy-btn {
			width: 356rpx;
			height: 80rpx;
			border-radius: 0 40rpx 40rpx 0;
			background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
			color: #fff;
		}

		.score-btn {
			width: 100%;
			margin: 0 20rpx;
			height: 80rpx;
			border-radius: 40rpx;
			background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
			color: #fff;
		}
	}

	.ss-modal-box {
		border-radius: 30rpx 30rpx 0 0;
		max-height: 1000rpx;

		.modal-header {
			position: relative;
			padding: 80rpx 20rpx 40rpx;

			.sku-image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 10rpx;
			}

			.header-right {
				height: 160rpx;
			}

			.close-icon {
				position: absolute;
				top: 10rpx;
				right: 20rpx;
				font-size: 46rpx;
				opacity: 0.2;
			}

			.goods-title {
				font-size: 28rpx;
				font-weight: 500;
				line-height: 42rpx;
			}

			.score-img {
				width: 36rpx;
				height: 36rpx;
				margin: 0 4rpx;
			}

			.score-text {
				font-size: 30rpx;
				font-weight: 500;
				color: $red;
				font-family: OPPOSANS;
			}

			.price-text {
				font-size: 30rpx;
				font-weight: 500;
				color: $red;
				font-family: OPPOSANS;

				&::before {
					content: '￥';
					font-size: 30rpx;
					font-weight: 500;
					color: $red;
				}
			}

			.stock-text {
				font-size: 26rpx;
				color: #999999;
			}
		}

		.modal-content {
			padding: 0 20rpx;

			.modal-content-scroll {
				max-height: 600rpx;

				.label-text {
					font-size: 26rpx;
					font-weight: 500;
				}

				.buy-num-box {
					height: 100rpx;
				}

				.spec-btn {
					height: 60rpx;
					min-width: 100rpx;
					padding: 0 30rpx;
					background: #f4f4f4;
					border-radius: 30rpx;
					color: #434343;
					font-size: 26rpx;
					margin-right: 10rpx;
					margin-bottom: 10rpx;
				}

				.disabled-btn {
					font-weight: 400;
					color: #c6c6c6;
					background: #f8f8f8;
				}
			}
		}
	}
</style>